package pers.jg.rrsserver.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.google.gson.GsonBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import pers.jg.rrsserver.entity.IndexCarousel
import pers.jg.rrsserver.entity.Policy
import pers.jg.rrsserver.mapper.IndexCarouselMapper
import pers.jg.rrsserver.mapper.UserMapper

@RestController
@CrossOrigin
class IndexCarouselController {

    class IndexCarouselList {
        lateinit var indexCarouselList: ArrayList<IndexCarousel>
    }

    @Autowired
    private lateinit var userMapper: UserMapper

    @Autowired
    private lateinit var indexCarouselMapper: IndexCarouselMapper

    @PostMapping("/api/postIndexCarouselList")
    fun postIndexCarouselList(
        @RequestBody data: String
    ): String {

        val gson = GsonBuilder().create()
        val indexCarouselList = gson.fromJson(data, IndexCarouselList::class.java)

        indexCarouselMapper.delete(null)

        val res = indexCarouselMapper.insert(indexCarouselList.indexCarouselList)
        println(res.size)

        return ""
    }

    @GetMapping("/api/getIndexCarouselList")
    fun getIndexCarouselList(
    ): String {
        val queryWrapper = QueryWrapper<IndexCarousel>()
        // 按照优先级升序排序
        queryWrapper.orderByAsc("priority")
        val indexCarouselList = indexCarouselMapper.selectList(queryWrapper)
//        indexCarouselList.forEach(System.out::println)

        val gson = GsonBuilder().create()
        return gson.toJson(indexCarouselList)
    }

    @PostMapping("/api/updateIndexCarousel")
    fun updateIndexCarousel(
        @RequestBody data: Map<String, Object>
    ): String {
        val indexCarousel = IndexCarousel()
        indexCarousel.id = data["id"].toString()
        indexCarousel.url = data["url"].toString()
        val res = indexCarouselMapper.updateById(indexCarousel)
        return "ok"
    }
}